/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jprojetopharmalink;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Rodrigo Toma
 */
public class Conexao {

    private static Connection conn;
    public static String servidor;

    public static Connection getConnetion() {

        FileReader arquivoCaminho = null;
        File cam = null;
        String pasta;
        try {
            cam = new File(new File(".").getCanonicalPath());
            arquivoCaminho = new FileReader(cam + "\\Servidor.TXT");
            BufferedReader buffer = new BufferedReader(arquivoCaminho);
            String linha = buffer.readLine();

            for (int i = 0; i < 1; i++) {

                if (i == 0) {
                    servidor = linha;

                }

            }

            buffer.close();

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Falta do arquivo Servidor.txt");
        } catch (IOException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                arquivoCaminho.close();
            } catch (IOException ex) {
                Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        try {
            String userName = "sa";
            String password = "1234";

            String url = "jdbc:sqlserver://" + servidor + "\\SQLEXPRESS;databaseName=pharmalink";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(url, userName, password);

            // System.out.println("Conexao ok");  

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;

    }
}
